unit main;

{$mode delphi}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, SynEdit, SynHighlighterHTML, Forms, Controls,
  Graphics, Dialogs, StdCtrls, ComCtrls, httpsend, XiPanel, LCLIntF, ExtCtrls,
  Menus, XiButton, ssl_openssl, xmlparser;

type

  { TfrmMain }

  TfrmMain = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    PopupMenu1: TPopupMenu;
    Splitter1: TSplitter;
    textHeaders: TSynEdit;
    textURL: TComboBox;
    listAgents: TComboBox;
    Panel1: TPanel;
    SaveDialog1: TSaveDialog;
    StatusBar1: TStatusBar;
    textResponse: TSynEdit;
    SynHTMLSyn1: TSynHTMLSyn;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { private declarations }
    bgPanel: TXiPanel;
    btnGo: TXiButton;
    btnSave: TXiButton;
    btnAbout: TXiButton;
  public
    { public declarations }
    appdir: String;
    UserAgents: TList;
    procedure LoadUserAgents;
    procedure SaveUserAgents;
  end;

  PUserAgent = ^TUserAgent;
  TUserAgent = record
    friendlyName: String;
    userAgent: String;
  end;


var
  frmMain: TfrmMain;

const
  APPNAME = 'HTTPres';
  CURRVER = 20150402;
  APPVER = '1.0';

implementation

{$R *.lfm}

uses about;

procedure httpGet(URL: String; userAgent: String; var response: String; var rescode: integer; var headers: String);
var
  HTTP: THTTPSend;
  l: TStrings;
  OS: String;
begin
  response := '';
  l := TStringList.Create;
  HTTP := THTTPSend.Create;
  HTTP.UserAgent := userAgent;
  if HTTP.HTTPMethod('GET', URL) then
  begin
    l.LoadFromStream(Http.Document);
    response := l.Text;
    rescode := HTTP.ResultCode;
    headers := HTTP.Headers.Text;
  end;
  HTTP.Free;
  l.Free;
end;

{ TfrmMain }

procedure TfrmMain.LoadUserAgents;
var
  Parser: TXMLParser;
  t: TStrings;
  ua,ft: String;
  newOne: Boolean;
  pa: PUserAgent;
begin
  if not FileExists(appdir + 'ua.xml') then exit;
  t := TStringList.Create;
  t.LoadFromFile(appdir + 'ua.xml');
  Parser := TXMLParser.Create(t.Text);
  newOne := false;
  while Parser.Next do
  begin
    if Parser.TagType = ttBeginTag then
    begin
      if Parser.Name = 'item' then
      begin
        newOne := true;
        ua := '';
        ft := '';
      end;
      if Parser.Name = 'useragent' then ua := Parser.ContentCode;
      if Parser.Name = 'title' then ft := Parser.ContentCode;
    end;
    if (Parser.TagType = ttEndTag) and (Parser.Name = 'item') then
    begin
      newOne := false;
      new(pa);
      pa^.userAgent:=ua;
      pa^.friendlyName:=ft;
      UserAgents.Add(pa);
    end;
  end;
  Parser.Free;
  t.Free;
end;

procedure TfrmMain.SaveUserAgents;
begin

end;

procedure TfrmMain.Button1Click(Sender: TObject);
var
  response: String;
  headers: String;
  resultcode: integer;
begin
  response := '';
  resultcode := 0;
  textResponse.ClearAll;
  if listAgents.ItemIndex < 0 then listAgents.ItemIndex := 0;
  httpGet(textURL.Text, agents[listAgents.ItemIndex].userAgent, response, resultcode, headers);
  textResponse.Text:=response;
  textHeaders.Text := headers;
  StatusBar1.SimpleText:= 'Server returned code '+IntToStr(resultcode);
end;

procedure TfrmMain.Button2Click(Sender: TObject);
begin
  if SaveDialog1.Execute then
    textResponse.Lines.SaveToFile(SaveDialog1.FileName);
end;

procedure TfrmMain.Button3Click(Sender: TObject);
begin
  // Show about form
  frmAbout.ShowModal;
end;

procedure TfrmMain.FormCreate(Sender: TObject);
begin
{  agents[0].friendlyName := 'Chrome/Linux';
  agents[0].userAgent := 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11';
  listAgents.Items.Add(agents[0].friendlyName);
  agents[1].friendlyName := 'Chrome/Windows';
  agents[1].userAgent := 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.60 Safari/537.17';
  listAgents.Items.Add(agents[1].friendlyName);
  agents[2].friendlyName := 'Chrome/OSX';
  agents[2].userAgent := 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17';
  listAgents.Items.Add(agents[2].friendlyName);
  agents[3].friendlyName := 'Firefox/Linux';
  agents[3].userAgent:= 'Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:17.0) Gecko/20100101 Firefox/17.0';
  listAgents.Items.Add(agents[3].friendlyName);
  agents[4].friendlyName := 'Firefox/Windows';
  agents[4].userAgent := 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:16.0.1) Gecko/20121011 Firefox/16.0.1';
  listAgents.Items.Add(agents[4].friendlyName);
  agents[5].friendlyName := 'Firefox/OSX';
  agents[5].userAgent := 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:15.0) Gecko/20100101 Firefox/15.0.1';
  listAgents.Items.Add(agents[5].friendlyName);
  agents[6].friendlyName := 'Mobile Safari/BlackBerry';
  agents[6].userAgent := 'Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.1.0.346 Mobile Safari/534.11+';
  listAgents.Items.Add(agents[6].friendlyName);
  agents[7].friendlyName := 'Mobile Safari/Android';
  agents[7].userAgent := 'Mozilla/5.0 (Linux; U; Android 2.2.1; en-gb; HTC_DesireZ_A7272 Build/FRG83D) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1';
  listAgents.Items.Add(agents[7].friendlyName);
  agents[8].friendlyName := 'Mobile Safari/iPhone';
  agents[8].userAgent := 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_2_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5';
  listAgents.Items.Add(agents[8].friendlyName);
  agents[9].friendlyName := 'Safari/OSX';
  agents[9].userAgent := 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/536.26.17 (KHTML, like Gecko) Version/6.0.2 Safari/536.26.17';
  listAgents.Items.Add(agents[9].friendlyName);
  agents[10].friendlyName:= 'Internet Exploder/Windows';
  agents[10].userAgent := 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)';
  listAgents.Items.Add(agents[10].friendlyName);
  agents[11].friendlyName := 'Konqueror/Linux';
  agents[11].userAgent := 'Mozilla/5.0 (X11; Linux) KHTML/4.9.1 (like Gecko) Konqueror/4.9';
  listAgents.Items.Add(agents[11].friendlyName);}
  bgPanel := TXiPanel.Create(Self);
  bgPanel.Parent := frmMain;
  bgPanel.Align := alTop;
  bgPanel.Height := Panel1.Height;
  bgPanel.ColorScheme := XiPanel.csSky;
  bgPanel.BevelOuter:=bvNone;
  Panel1.Visible := false;
  textURL.Parent := bgPanel;
  listAgents.Parent := bgPanel;
  btnGo := TXiButton.Create(Self);
  btnGo.Parent := bgPanel;
  btnGo.Top := Button1.Top;
  btnGo.Left := Button1.Left;
  btnGo.Height := Button1.Height;
  btnGo.Width := Button1.Width;
  btnGo.Caption := 'Go';
  btnGo.ColorScheme := csNeoGrass;
  btnGo.OnClick := Button1Click;
  btnSave := TXiButton.Create(Self);
  btnSave.Parent := bgPanel;
  btnSave.Top := Button2.Top;
  btnSave.Left := Button2.Left;
  btnSave.Height := Button2.Height;
  btnSave.Width := Button2.Width;
  btnSave.Caption := 'Save';
  btnSave.ColorScheme := csNeoSky;
  btnSave.OnClick := Button2Click;
  btnAbout := TXiButton.Create(Self);
  btnAbout.Parent := bgPanel;
  btnAbout.Top := Button3.Top;
  btnAbout.Left := Button3.Left;
  btnAbout.Height := Button3.Height;
  btnAbout.Width := Button3.Width;
  btnAbout.Caption := 'About';
  btnAbout.ColorScheme := csNeoSky;
  btnAbout.OnClick := Button3Click;
  frmMain.Caption := APPNAME + ' ' + APPVER;
  Application.Title := APPNAME;
  appdir :=  GetUserDir + '.httpres' + PathDelim;
  if not DirectoryExists(appdir) then mkdir(appdir);
  UserAgents := TList.Create;
end;

procedure TfrmMain.FormDestroy(Sender: TObject);
begin
  UserAgents.Free;
end;

end.

